<template>
  <div class="setting-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first" class="tab-pane">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRolesFn"
              >新增角色</el-button>
            </el-row>
            <!-- 使用 Table 组件实现用户角色的渲染 -->
            <el-table :data="rolesList" border style="width: 100%">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="small"
                    type="success"
                    @click="setRoles(scope.row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRoles(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRoles(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[2, 3, 5, 10]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" class="tab-pane">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="CompanyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="CompanyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="CompanyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="CompanyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 点击出现弹窗 -->
        <el-dialog
          :title="isEdit ? '编辑角色' : '新增角色'"
          :visible.sync="dialogVisible"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          @close="closeFn"
        >
          <el-form
            ref="roleform"
            :rules="roleRules"
            :model="roleform"
            label-width="80px"
          >
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleform.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleform.description" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="roleSubmit">{{
                isEdit ? "修改" : "确定"
              }}</el-button>
              <el-button @click="roleCancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
        <!-- 配置权限的弹框 -->
        <el-dialog
          title="提示"
          :visible.sync="showAssper"
          width="50%"
        >
          <AssignPermission v-if="showAssper" :uid="userId" @close="showAssper=false" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  addRoleApi,
  delRoleInfoApi,
  editRoleInfoApi,
  getCompanyInfoApi,
  getRoleApi,
  getRoleInfoApi
} from '@/api/setting'
import { mapGetters } from 'vuex'
import AssignPermission from './assignPermission.vue'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      query: {
        page: 1, // 当前页面
        pagesize: 2 // 页面显示的条数
      },
      rolesList: [], // 角色列表
      total: 0, // 角色数据总条数
      CompanyInfo: {}, // 公司信息
      dialogVisible: false, // 对话框开启关闭
      showAssper: false,
      roleform: {
        name: '',
        description: ''
      }, // 人员信息列表
      roleRules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }, // 验证规则列表
      isEdit: false,
      userId: ''
    }
  },
  computed: {
    ...mapGetters(['companyId']) // 公司id
  },
  created() {
    this.getRole()
    this.getCompanyInfo()
  },
  methods: {
    // 提交按钮
    roleSubmit() {
      // 点击提交按钮兜底验证
      this.$refs.roleform.validate(async(valid) => {
        if (!valid) return
        // 进行判断
        // 调用添加/修改角色接口
        const res = this.isEdit
          ? await editRoleInfoApi({
            companyId: this.companyId,
            ...this.roleform
          })
          : await addRoleApi(this.roleform)

        // 请求发送成功
        this.$message.success(res.message)
        this.getRole()
        this.dialogVisible = false
      })
    },
    // 取消按钮
    roleCancel() {
      this.dialogVisible = false
    },
    // 封装获取公司信息方法
    async getCompanyInfo() {
      const res = await getCompanyInfoApi(this.companyId)
      this.CompanyInfo = res.data
    },
    // 封装获取角色列表的方法
    async getRole() {
      const res = await getRoleApi(this.query)
      this.rolesList = res.data.rows
      this.total = res.data.total
    },
    // 新增角色
    addRolesFn() {
      this.dialogVisible = true
      this.isEdit = false
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(val) {
      // 将最新值赋值给原数值
      this.query.pagesize = val
      // 重新调用
      this.getRole()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(val) {
      this.query.page = val
      // 重新调用
      this.getRole()
    },

    // 设置角色
    setRoles(id) {
      // 开启弹框
      this.showAssper = true
      // 将id值
      this.userId = id
    },

    // 编辑角色
    async editRoles(obj) {
      this.dialogVisible = true
      this.isEdit = true
      const res = await getRoleInfoApi(obj.id)
      this.roleform = res.data
    },

    // 删除角色
    async delRoles(id) {
      // 弹出对话框
      const res = await this.$confirm(
        '此操作将永久删除该角色, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (res === 'confirm') {
        const res = await delRoleInfoApi(id)
        // 如果
        if (this.rolesList.length === 1) {
          // 如果已经是最后一页,那么直接返回自己,无需减一
          this.query.page > 1 ? this.query.page-- : this.query.page
        }
        this.$message.success(res.message)
        this.getRole()
      } else {
        this.$message('已取消删除')
      }
    },
    closeFn() {
      this.roleform = {
        name: '',
        description: ''
      }
      this.$refs.roleform.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px;
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
}
form.el-form {
  padding: 0 40px 0 20px;
}
</style>
